import { BasicColumn, FormSchema } from '@apps/antd-admin/src/components/table';
import { render } from '@apps/antd-admin/src/hooks/render/renderUtils';
import { categoryList } from '@apps/ant-bus/src/views/business/goods/category/api';
import { RenderType } from '@pkg/setting';
import { companyList } from '@apps/ant-bus/src/views/business/companyManage/company/data/api';

export const columns: BasicColumn[] = [
  {
    title: '创建日期',
    dataIndex: 'createTime',
    width: 180,
  },
  {
    title: '下单企业',
    dataIndex: 'companyName',
    width: 150,
  },
  {
    title: '订单号',
    dataIndex: 'orderNo',
    width: 170,
  },
  {
    title: '商品名称',
    dataIndex: 'goodsName',
    width: 220,
  },
  {
    title: '商品单价',
    dataIndex: 'unitPrice',
    width: 100,
  },
  {
    title: '地址',
    dataIndex: 'address',
    width: 350,
  },
  {
    title: '营业时间',
    dataIndex: 'businessTime',
    width: 300,
  },
  {
    title: '封面图',
    dataIndex: 'coverImg',
    width: 100,
    customRender: render.renderImage,
    isExport: false,
  },
  {
    title: '下单数量',
    dataIndex: 'buyNum',
    width: 100,
  },
  {
    title: '订单总额',
    dataIndex: 'totalPrice',
    width: 100,
  },
  {
    title: '支付状态',
    dataIndex: 'orderStatus',
    dictData: [
      { label: '待付款', value: '1', color: '#a6a9a6' },
      { label: '已付款', value: '2', color: '#09d550' },
      { label: '已退款', value: '3', color: '#095ad5' },
      { label: '申请退款中', value: '4', color: '#f31111' },
    ],
    width: 120,
    renderType: RenderType.DICT,
  },
  {
    title: '支付类型',
    dataIndex: 'payTypeDesc',
    width: 120,
  },
  {
    title: '操作人',
    dataIndex: 'optUserName',
    width: 130,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'startDate',
    label: '开始日期',
    component: 'DatePicker',
    colProps: { span: 5 },
    componentProps: {
      valueFormat: 'YYYY-MM-DD',
      format: 'YYYY-MM-DD',
    },
  },
  {
    field: 'endDate',
    label: '结束日期',
    component: 'DatePicker',
    colProps: { span: 5 },
    componentProps: {
      valueFormat: 'YYYY-MM-DD',
      format: 'YYYY-MM-DD',
    },
  },
  {
    label: '合作企业',
    field: 'companyId',
    component: 'ApiSelect',
    required: false,
    componentProps: {
      api: companyList,
      params: { handlerName: 'baseManageUserCompanyListHandler' },
      labelField: 'companyName',
      valueField: 'companyId',
    },
  },
  {
    field: 'orderNo',
    label: '订单编号',
    component: 'Input',
    colProps: { span: 4 },
  },
  {
    field: 'orderStatus',
    label: '订单状态',
    component: 'Select',
    colProps: { span: 4 },
    componentProps: {
      options: [
        { label: '待付款', value: 1 },
        { label: '已付款', value: 2 },
        { label: '已退款', value: 3 },
        { label: '申请退款中', value: 4 },
        { label: '已关闭', value: 5 },
      ],
    },
  },
  {
    field: 'categoryId',
    label: '商品类型',
    component: 'ApiSelect',
    colProps: { span: 4 },
    componentProps: {
      api: categoryList,
      labelField: 'categoryName',
      valueField: 'categoryId',
    },
  },
  {
    field: 'goodsName',
    label: '商品名称',
    component: 'Input',
    colProps: { span: 4 },
  },
];

const setCompanyComponentProps = (companyId: string) => {
  return {
    multiple: '',
    params: { goodsState: 1, companyId: companyId },
    selectConfig: {
      api: '/companyGoods/page',
      rowKey: 'goodsId',
      labelKey: 'goodsNameDesc',
      modalTitle: '商品选择',
      searchForm: [
        {
          label: '商品分类',
          field: 'categoryId',
          component: 'ApiSelect',
          componentProps: {
            api: categoryList,
            labelField: 'categoryName',
            valueField: 'categoryId',
          },
        },
        {
          label: '商品名称',
          field: 'goodsName',
          component: 'Input',
        },
      ],
      columns: [
        {
          title: '商品封面',
          dataIndex: 'coverImg',
          width: 80,
          customRender: render.renderImage,
        },
        {
          title: '商品名称',
          dataIndex: 'goodsName',
          width: 150,
        },
        {
          title: '商品价格',
          dataIndex: 'goodsPrice',
          width: 100,
        },
      ],
    },
  };
};

export const formSchema: FormSchema[] = [
  {
    label: '合作企业',
    field: 'companyId',
    component: 'ApiSelect',
    required: true,
    componentProps: ({ formActionType, formModel }) => {
      return {
        api: companyList,
        params: { handlerName: 'baseUserCompanyListHandler' },
        labelField: 'companyDesc',
        valueField: 'companyId',
        defaultActiveFirstOption: true,
        onChange(companyId) {
          const { updateSchema, setFieldsValue } = formActionType;
          setFieldsValue({ goodsId: null });
          let componentProps = setCompanyComponentProps(companyId);
          updateSchema({
            field: 'goodsId',
            componentProps: componentProps,
          });
        },
      };
    },
  },
  {
    label: '商品',
    field: 'goodsId',
    component: 'ApiModalSelect',
    required: true,
    ifShow: ({ values }) => {
      if (values.companyId) {
        return true;
      }
      return false;
    },
    componentProps: ({ formModel }) => {
      let componentProps = setCompanyComponentProps(formModel.companyId);
      return componentProps;
    },
  },
  {
    label: '商品数量',
    field: 'goodsNum',
    component: 'InputNumber',
    required: true,
    defaultValue: 1,
  },
];
